<?php
namespace app\pay\controller\huifubao;

use think\Controller;
use pay\huifubao\RequestHandler;
use pay\huifubao\Config;

class Test extends Controller {
//    public function test() {
//        //获取ip
//	$user_ip = "";
//	if(isset($_SERVER['HTTP_CLIENT_IP']))
//	{
//		$user_ip = $_SERVER['HTTP_CLIENT_IP'];
//	}
//	elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
//	{
//		$user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
//	}
//	else
//	{
//		$user_ip = $_SERVER['REMOTE_ADDR'];
//	}
//        dump($user_ip);
//    }

    private $reqHandler = null;
    private $cfg = null;
    
    public function request() {
        $this->reqHandler = new RequestHandler();
        $this->cfg = new Config();
    }

    public function index() {
        date_default_timezone_set('Asia/Shanghai');
        $outNo = 'CY' . date('YmdHis', time()) . rand(10, 99);
        $this->assign('timestamp', time());
        $this->assign('outNo', $outNo);
        $this->assign('dateTime', date('YmdHis', time()));
        return $this->fetch();
    }
    
    public function bankid() {
        $this->request();
        $data['merchantId'] = $this->cfg->C('merchantId');
        $result = $this->reqHandler->curl($this->cfg->C('onlineBankListUrl'), $data);
        dump($this->cfg->C('onlineBankListUrl'));
        dump($data);
        return json_encode($result, 320);
    }
    
   /*
    * 汇付宝直接下单接口
    */
    public function order() {
        $this->request();
        $data = input('post.');
        $data['merchantId'] =  $this->cfg->C('merchantId'); // 必填项，商户号，由支付平台分配
        $data['version'] = $this->cfg->C('version'); // 接口版本号
        $data['productCode'] = $this->cfg->C('productCode'); // 产品编码，用户签约的产品编码
        $data['sign'] = $this->reqHandler->createSign($data); // 创建签名
        dump($data);
        $result = $this->reqHandler->curl($this->cfg->C('orderUrl'), $data);
        return json_encode($result, 320);
    }
}

